﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;

namespace TFhisto.UI
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class AddFile : Window
    {
        private bool isInEditMode = false;
        private string initialPath;

        public AddFile()
        {
            InitializeComponent();
            ErrorMessage.Visibility = Visibility.Hidden;

            isInEditMode = false;
            btnAdd.Content = "Add file";
            btnAddAndClose.Content = "Add & Close";
        }

        public AddFile(string filePath) : this()
        {
            isInEditMode = true;
            initialPath = filePath;
            FilePath.Text = filePath;

            btnAdd.Content = "Re-initialize";
            btnAddAndClose.Content = "Save & Close";
        }

        private bool CheckFile()
        {
            bool isCorrect = CommonUIHelper.CheckItem(FilePath.Text);
            
            if (isCorrect)
            {
                ErrorMessage.Text = "This path is correct, click Add to proceed.";
            }
            else
            {
                ErrorMessage.Text = "Please check the path consistency.";
            }

            ErrorMessage.Visibility = Visibility.Visible;
            return isCorrect;
        }

        private void btnCheck_Click(object sender, RoutedEventArgs e)
        {
            CheckFile();
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (isInEditMode)
            {
                FilePath.Text = initialPath;
            }
            else
            {
                if (CheckFile())
                {
                    CommonUIHelper.AddFile(FilePath.Text);
                }
            }
        }

        private void btnAddAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (isInEditMode)
            {
                if (CheckFile())
                {
                    CommonUIHelper.ModifyFile(initialPath, FilePath.Text);
                }
            }
            else
            {
                btnAdd_Click(sender, e);
            }
            this.Close();
        }
    }
}